import requests
import json
import time

url = 'https://m.weibo.cn/api/container/getIndex?type=uid&value=5745966493&containerid=1076035745966493'
# url = 'https://m.weibo.cn/api/container/getIndex?topnav=1&wvr=6&jumpfrom=weibocom&type=uid&value=2316642707&containerid=1076032316642707'

existed_content = []

except_content = '2019-12-05'

i = 0

while True:
    i += 1

    resp_str = requests.get(url)

    content_json = json.loads(resp_str.text).get('data')

    cards = content_json.get('cards')
    if len(cards) > 0:
        j = len(cards)
        while j > 0:
            j = j - 1
            cart_type = cards[j].get('card_type')
            if cart_type == 9:
                mblog = cards[j].get('mblog')
                created_at = mblog.get('created_at')
                text = mblog.get('text')
                try:
                    if '买' in text or '卖' in text or '仓位' in text:
                        if text not in existed_content and created_at != except_content:
                            existed_content.append(text)
                            print('\n' + "发布时间：" + str(created_at) + "\n" + "微博内容：" + text + '\n')
                except Exception as e:
                    continue

        sleep_value = 2
        if i % 10 == 0:
            sleep_value = 5
        time.sleep(sleep_value)
    else:
        break
#
# since_id = ''
#
# while True:
#
#     req_url = url
#
#     if since_id != '':
#         req_url += '&since_id=' + str(since_id)
#
#     resp_str = requests.get(req_url)
#
#     content_json = json.loads(resp_str.text).get('data')
#
#     cardlistInfo = content_json.get('cardlistInfo')
#
#     since_id = cardlistInfo.get('since_id')
#
#     cards = content_json.get('cards')
#     if len(cards) > 0:
#         j = len(cards)
#         while j > 0:
#             j = j - 1
#             cart_type = cards[j].get('card_type')
#             if cart_type == 9:
#                 mblog = cards[j].get('mblog')
#                 created_at = mblog.get('created_at')
#                 text = mblog.get('text')
#                 try:
#                     if '买入' in text or '卖出' in text:
#                         if text not in existed_content and created_at != except_content:
#                             existed_content.append(text)
#                             print('\n' + "发布时间：" + str(created_at) + "\n" + "微博内容：" + text + '\n')
#                 except Exception as e:
#                     continue
#         time.sleep(1)
#     else:
#         break
